//
//  ModifyViewController.m
//  
//
//  Created by lanou3g on 16/1/16.
//
//

#import "ModifyViewController.h"
#import "Mothy.h"
#import "CoreDataHandle.h"
@interface ModifyViewController ()<UITextFieldDelegate>

@property (nonatomic,strong)  UIBarButtonItem *buttonItem;
@property(nonatomic,strong)NSString *string;
@property(nonatomic,strong)NSString *str;
@end

@implementation ModifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"修改账目";
    
    
    UIBarButtonItem *liftItem=[[UIBarButtonItem alloc]initWithTitle:@"取消"style:UIBarButtonItemStylePlain target:self action:@selector(liftItemActon:)];
    self.navigationItem.leftBarButtonItem=liftItem;
    
    
    self.buttonItem=[[UIBarButtonItem alloc]initWithTitle:@"修改"style:UIBarButtonItemStylePlain target:self action:@selector(buttonItem:)];
    self.navigationItem.rightBarButtonItem=self.buttonItem;
    self.buttonItem.enabled=NO; //不能在被点击
    self.nameTextFiled.delegate=self;
    self.mothyTextFiled.delegate=self;
    self.mothyTextFiled.placeholder=@"红包金额:";
    self.nameTextFiled.keyboardType= UIKeyboardTypeDefault;
    self.mothyTextFiled.keyboardType=UIKeyboardTypeNumberPad;
    UITapGestureRecognizer *taypGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGRAction:)];
    [self.liftView addGestureRecognizer:taypGR];
    UITapGestureRecognizer *rightTapGR=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightTapGRAction:)];
    [self.rightImage addGestureRecognizer:rightTapGR];
    [self setModle];
}
-(void)setModle{
    self.str=self.modle.name;
    self.string=self.modle.picture;
    self.nameTextFiled.text=self.modle.name;
    self.mothyTextFiled.text=self.modle.closed;
    if ([self.modle.picture isEqualToString:@"type_big_0"]) {
        self.shouruImage.image=[UIImage imageNamed:@"type_big_0"];
        self.zhichuImage.image=[UIImage imageNamed:@"chose_business_normal@2x"];
    }else{
        self.shouruImage.image=[UIImage imageNamed:@"chose_business_normal@2x"];
        self.zhichuImage.image=[UIImage imageNamed:@"type_big_24"];
    }
}
-(void)tapGRAction:(UITapGestureRecognizer*)sender{
    if ([self.shouruImage.image isEqual:[UIImage imageNamed:@"type_big_0"]]) {
        self.nameTextFiled.placeholder=@"给谁的:";
        self.shouruImage.image=[UIImage imageNamed:@"chose_business_normal@2x"];
        self.zhichuImage.image=[UIImage imageNamed:@"type_big_24"];
        self.string=@"type_big_24";
    }else{
        self.nameTextFiled.placeholder=@"收谁的:";
        self.shouruImage.image=[UIImage imageNamed:@"type_big_0"];
        self.zhichuImage.image=[UIImage imageNamed:@"chose_business_normal@2x"];
        self.string=@"type_big_0";
    }
}
-(void)rightTapGRAction:(UITapGestureRecognizer*)sender{
    if ([self.zhichuImage.image isEqual:[UIImage imageNamed:@"type_big_24"]]) {
        self.nameTextFiled.placeholder=@"收谁的:";
        self.shouruImage.image=[UIImage imageNamed:@"type_big_0"];
        self.zhichuImage.image=[UIImage imageNamed:@"chose_business_normal@2x"];
        self.string=@"type_big_0";
        
    }else{
        self.nameTextFiled.placeholder=@"给谁的:";
        self.shouruImage.image=[UIImage imageNamed:@"chose_business_normal@2x"];
        self.zhichuImage .image=[UIImage imageNamed:@"type_big_24"];
        self.string=@"type_big_24";
    }
}

-(void)liftItemActon:(UIBarButtonItem*)sender{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
//修改
-(void)buttonItem:(UIBarButtonItem*)sender{
    [[CoreDataHandle shareCoreDataHandle]getCoreData];
    NSFetchRequest *request=[[NSFetchRequest alloc]initWithEntityName:@"Mothy"];
    NSArray *objcArray=[[CoreDataHandle shareCoreDataHandle].context executeFetchRequest:request error:nil];
    for (Mothy *m in objcArray) {
        NSLog(@"++++++++++++++++++%@",m.name);
        NSLog(@"_________%@",self.str);
        if ([m.name  isEqualToString: self.str]) {
            [m setValue:self.nameTextFiled.text forKey:@"name"];
            [m setValue:self.mothyTextFiled.text forKey:@"closed"];
            [m setValue:self.string forKey:@"picture"];
        }
        //保存数据
        if ([[CoreDataHandle shareCoreDataHandle].context hasChanges]) {
            [[CoreDataHandle shareCoreDataHandle].context save:nil];
        }
    }
    
   
    
    
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([self.nameTextFiled isEqual:textField]) {
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(self.mothyTextFiled.text.length>0&&toBeString.length>0){
            self.buttonItem.enabled=YES;
        }
    }
    if ([self.mothyTextFiled isEqual:textField]) {
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(self.nameTextFiled.text.length>0&&toBeString.length>0){
            self.buttonItem.enabled=YES;
        }
    }
    if ([self.nameTextFiled isEqual:textField]) {
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(self.mothyTextFiled.text.length>0&&toBeString.length==0){
            self.buttonItem.enabled=NO;
            ;
        }
    }
    if ([self.mothyTextFiled isEqual:textField]) {
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if(self.nameTextFiled.text.length>0&&toBeString.length==0){
            self.buttonItem.enabled=NO;
        }
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
